
import requests
import json
import sys


class King(object):
    def __init__(self, word):
        self.url = 'https://fanyi.baidu.com/sug'
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0'
        }
        self.data = {
            "kw": word
        }

    def post_data(self):
        response = requests.post(self.url, data=self.data, headers=self.headers)
        return response.content

    def parse_data(self, data):
        # loads方法将json字符串转换成python字典
        dict_data = json.loads(data)
        print(dict_data['data'])

    def run(self):
        # 编写爬虫逻辑
        # url
        # headers
        # data字典
        # 发送请求获取响应
        response = self.post_data()
        print(response)
        # 数据解析
        # self.parse_data(response)


if __name__ == '__main__':
    word = input('请输入要翻译的单词或句子:')
    # word = sys.argv[1] #不会用
    king = King(word)
    king.run()